/*
 *  Copyright (c) 2009, Hugo Augusto Alves
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *      * Redistributions of source code must retain the above copyright
 *        notice, this list of conditions and the following disclaimer.
 *      * Redistributions in binary form must reproduce the above copyright
 *        notice, this list of conditions and the following disclaimer in the
 *        documentation and/or other materials provided with the distribution.
 *      * Neither the name of the Hugo Augusto Alves nor the
 *        names of its contributors may be used to endorse or promote products
 *        derived from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
 *  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 *  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 *  DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
 *  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 *  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.wiscriptor.model;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import org.wiscriptor.core.WiscriptorController;
import org.wiscriptor.lmldocument.OldLmlDocument;
import org.wiscriptor.internalization.I18N;
import org.wiscriptor.utils.TextTransfer;
import org.wiscriptor.utils.Utils;

/**
 *
 * @author Hugo
 */
public class DocModel {

    WiscriptorController controller;
    StringBuffer text;
    StringBuffer header;
    String name = I18N.getInstance().getValueForKey(I18N.key.stringNewFile);

    public DocModel(WiscriptorController controller) {
        this.controller = controller;
        text = new StringBuffer();
    }

    public DocModel(WiscriptorController controller, File file) {
        this(controller);
        name = file.getName();
        BufferedReader fileBuffer = Utils.getFileBuffer(file);
        try {
            while (fileBuffer.ready()) {
                text.append(fileBuffer.readLine());
                text.append('\n');
            }
            fileBuffer.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    /**
     * Cut
     * @param start The beginning index, inclusive.
     * @param end The ending index, exclusive.
     */
    public void cut(int start, int end) {
        if (start == end) {
            return;
        }
        TextTransfer.getInstance().setClipboardContents(text.substring(start, end).toString());
        text.delete(start, end);
    }

    /**
     * Copy
     * @param start The beginning index, inclusive.
     * @param end The ending index, exclusive.
     */
    public void copy(int start, int end) {
        if (start == end) {
            return;
        }
        TextTransfer.getInstance().setClipboardContents(text.substring(start, end).toString());
    }

    /**
     * Paste
     * @param start The beginning index, inclusive.
     * @param end The ending index, exclusive.
     */
    public int paste(int start, int end) {
        if (start != end) {
            text.delete(start, end);
        }
        text.insert(start, TextTransfer.getInstance().getClipboardContents());
        return TextTransfer.getInstance().getClipboardContents().length();
    }

    /**
     * Insert
     * @param start The beginning index, inclusive.
     * @param end The ending index, exclusive.
     * @param text The text to be inserted
     */
    public int insert(int start, int end, String txt) {
        if (start != end) {
            text.delete(start, end);
        }
        text.insert(start, txt);
        return text.length();
    }

    /**
     * Del
     * @param start The beginning index, inclusive.
     * @param end The ending index, exclusive.
     */
    public void del(int start, int end) {
        if (start == end) {
            return;
        }
        text.delete(start, end);
    }

    public String format(String txt, OldLmlDocument.marks m, int start, int end) {
        setText(txt);
        String mark = OldLmlDocument.getMark(m);
        if(start == end) insert(start, end, mark);
        else {
            text.insert(end, mark);
            text.insert(start, mark);
        }
        return getText();
    }

    public void setText(String txt) {
        Utils.erase(text);
        text.append(txt);
    }

    public String getText() {
        return text.toString();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}
